package gridmapper;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class RobotDataParser {

	public boolean writeData(DataOutputStream dataOut, RobotData robotData) {
		try {
			dataOut.writeInt(robotData.distanceNorth);
			dataOut.flush();
		} catch (IOException e) {
			System.err.println(e.getMessage());
			return false;
		}
		try {
			dataOut.writeInt(robotData.distanceEast);
			dataOut.flush();
		} catch (IOException e) {
			System.err.println(e.getMessage());
			return false;
		}
		try {
			dataOut.writeInt(robotData.distanceWest);
			dataOut.flush();
		} catch (IOException e) {
			System.err.println(e.getMessage());
			return false;
		}
		try {
			dataOut.writeInt(robotData.distanceSouth);
			dataOut.flush();
		} catch (IOException e) {
			System.err.println(e.getMessage());
			return false;
		}
		try {
			dataOut.writeInt(robotData.heading);
			dataOut.flush();
		} catch (IOException e) {
			System.err.println(e.getMessage());
			return false;
		}
		try {
			dataOut.writeInt(robotData.compassValue);
			dataOut.flush();
		} catch (IOException e) {
			System.err.println(e.getMessage());
			return false;
		}
		return true;
	}

	public RobotData readData(DataInputStream dataIn) {
		int distanceNorth = RobotData.ERROR_VALUE;
		int distanceEast = RobotData.ERROR_VALUE;
		int distanceWest = RobotData.ERROR_VALUE;
		int distanceSouth = RobotData.ERROR_VALUE;
		int heading = RobotData.ERROR_VALUE;
		int compassValue = RobotData.ERROR_VALUE;

		try {
			distanceNorth = dataIn.readInt();
		} catch (IOException e) {
			System.err.println(e.getMessage());
		}
		try {
			distanceEast = dataIn.readInt();
		} catch (IOException e) {
			System.err.println(e.getMessage());
		}
		try {
			distanceWest = dataIn.readInt();
		} catch (IOException e) {
			System.err.println(e.getMessage());
		}
		try {
			distanceSouth = dataIn.readInt();
		} catch (IOException e) {
			System.err.println(e.getMessage());
		}
		try {
			heading = dataIn.readInt();
		} catch (IOException e) {
			System.err.println(e.getMessage());
		}
		try {
			compassValue = dataIn.readInt();
		} catch (IOException e) {
			System.err.println(e.getMessage());
		}

		return new RobotData(distanceNorth, distanceEast, distanceWest,
				distanceSouth, heading, compassValue);
	}

}
